<?php

/* Main include. Some useful functions + lib loading */

@session_start(); // just if there is no access the php.ini

// Adding some functions

//  Geting user's IP
function get_ip()
{
    $ip = '';
    if( getenv('HTTP_FORWARDED_FOR'))
        $ip = getenv('HTTP_FORWARDED_FOR');
    elseif( getenv('HTTP_X_FORWARDED_FOR'))
        $ip = getenv('HTTP_X_FORWARDED_FOR');
    elseif( getenv('HTTP_X_COMING_FROM'))
        $ip = getenv('HTTP_X_COMING_FROM');
    elseif( getenv('HTTP_VIA'))
        $ip = getenv('HTTP_VIA');
    elseif( getenv('HTTP_XROXY_CONNECTION'))
        $ip = getenv('HTTP_XROXY_CONNECTION');
    elseif( getenv('HTTP_CLIENT_IP'))
        $ip = getenv('HTTP_CLIENT_IP');
    elseif( getenv('REMOTE_ADDR'))
        $ip = getenv('REMOTE_ADDR');
    else
        $ip = 'unknown';
return $ip;
}

//  Mysql errors with ip-log
/*
*$system_err - error description by a func like mysql_error()
*$func_argument - argument o a function which gave an error
*$description - your own descriotion of an error
*$b_iplog - bool
*/

function make_error($system_err, $func_argument, $description, $b_iplog)
{
    $reg_debug = 1; // 1-show all info about error, 0-only iplog+some cautions...
    $file_log = 'project_log.txt'; // Name of the file in cgi-bin to put the log in...

    if($reg_debug)
        $message ='<b> Regim debug: </b> <br> '.$system_err.' <br /><br />'.$description.':<br> <pre>'.$func_argument.'</pre>';
    else
        $message ='<b> Server accured an error... </b> <br /><br /> <h5> To be sure,we\'ve logged your ip to a log file. </h5>';
    echo $message;

    if($b_iplog)
    {
        // Save text to file // On Windows platform, use \n\r instead of \n
        $error_string = '['.mktime().rand(1,100).']: Mysql error from '.get_ip().': \''.$description.'\' ('.$func_argument.")\n\r";
        // info in log file
        $log = fopen( '../cgi-bin/'.$file_log , 'a' );
        fwrite($log, $error_string);
        fclose($log);
        die();
    }
}

// Loading functions handler
function load_lib($lib_name)
{
    if (is_array($lib_name))
    {
        foreach($lib_name as $name)
            include_once('classes/'.$name.'.php');
    }
    else
        include_once('classes/'.$lib_name.'.php');
}
 /*
// including userclass file
load_lib(array('User','DbMysql'));


$db = new DbMysql();
$u = new User();
echo $u->GetIp();
$u->Auth('Meanor', md5('ghjrcb'));
*/
?>